package com.cn.xact.ty_treasured_book.util.xmlparser;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.ParseException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.cn.xact.ty_treasured_book.config.TYConfigBase;

import android.util.Log;

public class ExtXMLUtil {

    public final static boolean DEBUG = TYConfigBase.XML_DEBUG;
    public static final String TAG = "ExtXMLUtil";

    public ExtXMLUtil ()
    {
        if (DEBUG) Log.v(TAG, "Create new ExtXMLUtil");
    }

    public ExtXMLParse ParseXMLFromPath (ExtXMLParse parseHandle, String parsePath)
    {
        if (DEBUG) Log.v(TAG, "Begin to parse XML from " + parsePath);
        int loopCnt = 0;

        try{
            InputStream myXMLINStream;
            HttpURLConnection myXMLConn;
            URL parseUri = new URL(parsePath);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            XMLReader saxReader = saxParser.getXMLReader();
            saxReader.setContentHandler(parseHandle);
            do
            {
                myXMLConn = (HttpURLConnection)parseUri.openConnection();
                myXMLINStream = myXMLConn.getInputStream();
            } while ((loopCnt < TYConfigBase.MAX_REQUEST_LOOP) && (myXMLConn.getContentLength() <= 0));
            saxReader.parse(new InputSource(myXMLINStream));
            myXMLINStream.close();
          } catch(ParseException e){
              e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
         } catch (SAXException e) {
            e.printStackTrace();
         } catch (ParserConfigurationException e) {
            e.printStackTrace();
         }

        return parseHandle;
    }

    public ExtXMLParse ParseXMLFromPathWithProxy (ExtXMLParse parseHandle, String parsePath)
    {
        int loopCnt = TYConfigBase.MAX_REQUEST_LOOP;

        if (DEBUG) Log.v(
                       TAG, "Begin to parse XML from "
                       + parsePath
                       + " with proxy "
                       //+ TYConfigBase.PROXY_SERVER
                       + ":"
                       //+ TYConfigBase.PROXY_PORT
                       );

        while (0 < loopCnt)
        {
            try{
                URL parseUri = new URL(parsePath);
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();
                XMLReader saxReader = saxParser.getXMLReader();
                saxReader.setContentHandler(parseHandle);
                saxReader.parse(new InputSource(parseUri.openStream()));
              } catch(Exception e){
                  if (DEBUG) Log.v(TAG, "Failed to parse the XML file");
                  e.printStackTrace();
                  loopCnt--;
              }
              loopCnt = 0;
        }
        return parseHandle;
    }

    /**
     * @deprecated
     * @param parseHandle
     * @param parseURL
     * @return
     */
    @Deprecated
    public ExtXMLParse ParseXMLFromURL (ExtXMLParse parseHandle, HttpURLConnection parseURL)
    {
        if (DEBUG) Log.v(TAG, "Begin to parse XML from URL");

        try{
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            XMLReader saxReader = saxParser.getXMLReader();
            saxReader.setContentHandler(parseHandle);
            saxReader.parse(new InputSource(parseURL.getInputStream()));
          } catch(Exception e){
            if (DEBUG) Log.v(TAG, "Failed to parse the XML file");
            e.printStackTrace();
          }

          return parseHandle;
    }
}